#include "Tabuleiro.h"

Tabuleiro::Tabuleiro()
{
	dimensao = 8;
	for(int i = 0; i < dimensao; i++)
	{
		vector<Casa> tmp;
		tmp.clear();
		for(int j =0; j < dimensao ; j++)
		{
			Casa c = Casa();
			if(i % 2 == 0)
			{
				if(j % 2 == 0)
					c.setCor(WHITE);
				else
					c.setCor(BLACK);
			}
			else
			{
				if(j % 2 == 1)
					c.setCor(WHITE);
				else
					c.setCor(BLACK);
			}
			tmp.push_back(c);
		}
		Matrix.push_back(tmp);
	}

	float BAmbient[4] = {0.0,0.0,0.0,0};
	float BDifuse[4] = {0.1,0.1,0.1,1.0};
	float BSpec[4] = {0.1,0.1,0.1,1.0};
	float BShineness = 1.0;
	base = new CGFappearance(BAmbient,BDifuse,BSpec,BShineness);
	base->setTexture("../textures/madeira.png");
	initTexturas();
	ti = 0;
	setTextura(ti);
	initTabuleiro();
}

void Tabuleiro::initTexturas()
{
	texturas.clear();
	texturas.push_back("wood.png");
	texturas.push_back("blank.png");
	texturas.push_back("dark.png");
	texturas.push_back("pasto.png");
}

void Tabuleiro::setNextTexture()
{
	ti = ti + 1;
	if(ti > (texturas.size()-1))
		ti = ti % texturas.size();
	setTextura(ti);
}

void Tabuleiro::initTabuleiro()
{
	pecas.clear();
	int tabb[8][8] = {{4,3,2,2,1,0,0,0},
					 {3,2,2,1,0,0,0,0},
					 {2,2,1,0,0,0,0,0},
					 {2,1,0,0,0,0,0,0},
					 {1,0,0,0,0,0,0,0},
					 {0,0,0,0,0,0,0,0},
					 {0,0,0,0,0,0,0,0},
					 {0,0,0,0,0,0,0,0}};	
	int tabr[8][8] = {{0,0,0,0,0,0,0,0},
					 {0,0,0,0,0,0,0,0},
					 {0,0,0,0,0,0,0,0},
					 {0,0,0,0,0,0,0,1},
					 {0,0,0,0,0,0,1,2},
					 {0,0,0,0,0,1,2,2},
					 {0,0,0,0,1,2,2,3},
					 {0,0,0,1,2,2,3,4}};
	vector<vector<Peca> > linha;
	for(int i = 0; i < 8; i++)
	{
		linha.clear();
		for(int j = 0; j < 8; j++)
		{
			vector<Peca> casa;
			for(int k = 0; k < tabb[j][i]; k++)
			{
				Peca p = Peca(PRETO);
				casa.push_back(p);
			}
			for(int k = 0; k < tabr[j][i]; k++)
			{
				Peca p = Peca(VERMELHO);
				casa.push_back(p);
			}
			linha.push_back(casa);
		}
		pecas.push_back(linha);
	}
}

void Tabuleiro::setTextura(int tex)
{
	string t = "../textures/" + texturas[tex];
	textura = new CGFtexture(t);
	fun = new Fundo(textura);
}

vector<Peca> Tabuleiro::getCasa(int x, int y)
{
	return pecas[x][y];
}

void Tabuleiro::setCasa(int x, int y, vector<Peca> p)
{
	for(unsigned int i = 0; i < p.size(); i++)
	{
		pecas[x][y].push_back(p[i]);
	}
}

void Tabuleiro::clearCasa(int x, int y)
{
	pecas[x][y].clear();
}

void Tabuleiro::increaseCasa(int x, int y, int color)
{
	Peca p = Peca(color);
	pecas[x][y].push_back(p);
}

void Tabuleiro::decreaseCasa(int x, int y)
{
	pecas[x][y].pop_back();
}

Peca Tabuleiro::getTopPeca(int x, int y)
{
	return pecas[x][y].back();
}

bool Tabuleiro::isOwner(int x, int y, int player)
{
	return (pecas[x][y][pecas[x][y].size()-1].getCor() == player);
}

bool Tabuleiro::isKing(int x, int y)
{
	if(pecas[x][y].size() >= 4)
	{
		int correi = pecas[x][y][0].getCor();
		unsigned int i = 1;
		while(i < pecas[x][y].size())
		{
			if(pecas[x][y][i].getCor() == correi)
				i++;
			else
				return false;
		}
		return true;
	}
	else
		return false;
}

bool Tabuleiro::hasKings(int player)
{
	for(unsigned int x = 0; x < dimensao; x++)
	{
		for(unsigned int y = 0; y < dimensao; y++)
		{
			if(isKing(x,y) && isOwner(x,y,player))
				return true;
		}
	}
	return false;
}

void Tabuleiro::selectCasa(int x,int y)
{
	Matrix[x][y].select();
}

void Tabuleiro::deselectCasas()
{
	for(int i = 0; i < dimensao; i++)
	{
		for(int j = 0; j < dimensao; j++)
		{
			Matrix[i][j].deselect();
		}
	}
}

void Tabuleiro::draw()
{
	glPushMatrix();
		float TAmbient[4] = {0.0,0.0,0.0,0};
		float TDifuse[4] = {1,1,1,0.0};
		float TSpec[4] = {0.5,0.5,0.5,0.0};
		float TShineness = 10.0;
		CGFappearance* null = new CGFappearance(TAmbient,TDifuse,TSpec,TShineness);
		null->apply();
		fun = new Fundo(textura);
		glScaled(dimensao,1,dimensao);
		glTranslatef(0,0.2,0);
		glRotatef(90,0,1,0);
		fun->draw();
	glPopMatrix();

	glPushMatrix();
		base->apply();
		glScalef((dimensao+0.5),0.2,(dimensao+0.5));
		glutSolidCube(1);
	glPopMatrix();
	
	glPushMatrix();
	glTranslatef((-dimensao/2.0)+0.5,0.2,(-dimensao/2.0)+0.5);
	for(int i = 0; i < dimensao; i++)
	{
		glPushMatrix();	
		for(int j = 0; j < dimensao; j++)
			{	
				glPushMatrix();
				glTranslated(i*((double)dimensao/8.0),0.01,j*((double)dimensao/8.0));
				glPushName(i);
				glPushName(j);
				Matrix[i][j].draw();
				glPopName();
				glPopName();
				for(unsigned int k = 0; k < pecas[i][j].size(); k++)
				{
					glPushMatrix();
					glTranslatef(0,0.25*k,0);
					pecas[i][j][k].draw();
					glPopMatrix();
				}
				glPopMatrix();
			}
		glPopMatrix();
	}
	glPopMatrix();
}